package com.tcgroup.phone.base_core.widget.dialog.bottom;

import android.content.res.Configuration;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearSnapHelper;
import androidx.recyclerview.widget.RecyclerView;

import com.azoft.carousellayoutmanager.CarouselLayoutManager;
import com.azoft.carousellayoutmanager.CarouselZoomPostLayoutListener;
import com.azoft.carousellayoutmanager.CenterScrollListener;
import com.tcgroup.phone.base_core.R;
import com.tcgroup.phone.base_core.widget.dialog.BaseDialogFragment;
import com.tcgroup.phone.base_core.widget.dialog.adapter.BottomRvAdapter;
import com.tcgroup.phone.base_core.widget.dialog.model.Dirc;
import com.tcgroup.phone.base_core.widget.dialog.provider.DialogDataProvider;

import java.util.ArrayList;

/**
 * 作者: wangfei
 * 创建时期: 2020/5/14.
 * 包名: com.tcgroup.phone.base_core.widget.dialog.bottom
 * 类名: OSAndroidMvvm
 */
public class BottomDialogFragment extends BaseDialogFragment {

    public TextView tv_cancel;
    public TextView tv_define;
    public RecyclerView recyclerView;
    private BottomRvAdapter mBottomRvAdapter;
    public ChooseItemClick mChooseItemClick;
    public Dirc mSelectDirc;
    public ArrayList<Dirc> currentList;
    public DialogDataProvider mProvider;

    private BottomDialogFragment(DialogDataProvider provider){
        mProvider = provider;
    }


    @Override
    public void onStart() {
        super.onStart();
        mWindow.setGravity(Gravity.BOTTOM);
        mWindow.setWindowAnimations(R.style.BottomAnimation);
        mWindow.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, mWidthAndHeight[1] / 3);
    }

    @Override
    public int setLayoutId() {
        return R.layout.core_dialog_bottom_screen;
    }

    @Override
    public void initData() {
        currentList = mProvider.getDircList();
        mBottomRvAdapter.setData(currentList);
    }

    @Override
    public void initView() {

        tv_cancel = mRootView.findViewById(R.id.tv_cancel);
        tv_define = mRootView.findViewById(R.id.tv_define);
        recyclerView = mRootView.findViewById(R.id.recyclerView);

        mBottomRvAdapter = new BottomRvAdapter(getContext());
        recyclerView.setAdapter(mBottomRvAdapter);

        CarouselLayoutManager layoutManager = new CarouselLayoutManager(CarouselLayoutManager.VERTICAL,true);
        layoutManager.setPostLayoutListener(new CarouselZoomPostLayoutListener());
        layoutManager.setMaxVisibleItems(1);

        recyclerView.addOnScrollListener(new CenterScrollListener());
        recyclerView.setLayoutManager(layoutManager);
        LinearSnapHelper linearSnapHelper = new LinearSnapHelper();
        linearSnapHelper.attachToRecyclerView(recyclerView);

        layoutManager.addOnItemSelectionListener(adapterPosition -> {
            if(mBottomRvAdapter.mList.size() > 0){
                mSelectDirc = mBottomRvAdapter.mList.get(adapterPosition);
            }
        });

        tv_cancel.setOnClickListener(v -> dismissAllowingStateLoss());

        tv_define.setOnClickListener(v -> {
            if (mChooseItemClick != null){
                mChooseItemClick.itemClick(mSelectDirc);
            }
            dismissAllowingStateLoss();
        });

    }

    @Override
    public void onConfigurationChanged(@NonNull Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        mWindow.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, mWidthAndHeight[1] / 3);
    }


    public void setChooseItemClick(ChooseItemClick chooseItemClick){
        mChooseItemClick = chooseItemClick;
    }

    public interface ChooseItemClick{
        void itemClick(Dirc dirc);
    }
}
